package org.orz.cloud.lock.zookeeper;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.state.ConnectionState;
import org.apache.curator.framework.state.ConnectionStateListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Zookeeper链接状态变更的监听器
 */
public class ZkListener implements ConnectionStateListener {
	public static final Logger LOGGER = LoggerFactory.getLogger(ZkListener.class);

	/**
	 * 在链接状态发生变更的时候会调用此方法
	 * @param client 客户端对象
	 * @param state 变更后的新状态
	 */
	@Override
	public void stateChanged(CuratorFramework client, ConnectionState state) {
        if (state == ConnectionState.LOST) {
            //连接丢失  
			LOGGER.warn("DistributedLock lost session with zookeeper");
        } else if (state == ConnectionState.CONNECTED) {
            //连接新建  
			LOGGER.warn("DistributedLock connected with zookeeper");
        } else if (state == ConnectionState.RECONNECTED) {
        	//重新链接
			LOGGER.warn("DistributedLock reconnected with zookeeper");
        }
    } 
	

}
